﻿Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Imports System.Security.Cryptography
Imports System.IO

Public Module API

#Region " Definitions "

	Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal s As String) As Int32
	Private Declare Auto Function ScrollWindow Lib "user32.dll" (ByVal hWnd As Integer, ByVal XAmount As Integer, ByVal YAmount As Integer, <MarshalAs(UnmanagedType.Struct)> ByRef lpRect As RECT, <MarshalAs(UnmanagedType.Struct)> ByRef lpClipRect As RECT) As Integer

	<StructLayout(LayoutKind.Sequential)>
	Private Structure RECT
		Public Left As Integer
		Public Top As Integer
		Public Right As Integer
		Public Bottom As Integer
	End Structure

#End Region

	<Extension()>
	Public Function SendMsg(ByVal hWnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal s As String) As Int32
		Return SendMessage(hWnd, wMsg, wParam, s)
	End Function

	<Extension()>
	Public Function Scroll(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal Rect As Rectangle, ByVal ClipRect As Rectangle) As Int32
		Dim R1 As New RECT With {.Left = Rect.Left, .Top = Rect.Top, .Bottom = Rect.Bottom, .Right = Rect.Right}
		Dim R2 As New RECT With {.Left = ClipRect.Left, .Top = ClipRect.Top, .Bottom = ClipRect.Bottom, .Right = ClipRect.Right}
		Return ScrollWindow(hWnd, x, y, R1, R2)
	End Function

	Sub Info(ByVal Message As String)
		MsgBox(Message, MsgBoxStyle.Information, "Bilgi")
	End Sub

	Sub Warning(ByVal Message As String)
		MsgBox(Message, MsgBoxStyle.Exclamation, "Uyarı")
	End Sub

	Function Confirm(ByVal Message As String) As Boolean
		Return MsgBox(Message, MsgBoxStyle.Question Or MsgBoxStyle.OkCancel, "Onay") = MsgBoxResult.Ok
	End Function

	Function Check(ByVal Control As Control, ByVal Condition As Boolean, ByVal Message As String) As Boolean
		If Not Condition Then Return False
		Control.Focus()
		Warning(Message)
		Return True
	End Function

	Function IsNull(ByVal v As Object, ByVal Def As Object) As Object
		Return If(v Is Nothing OrElse IsDBNull(v), Def, v)
	End Function

	Public Function EncrDecr(ByVal Encrypt As Boolean, ByVal Text As String) As String
		Dim Key As Byte() = System.Text.Encoding.UTF8.GetBytes("4a3jh2p4")
		Dim IV As Byte() = {18, 52, 86, 120, 144, 171, 205, 239}
		Dim Encryptor As New DESCryptoServiceProvider()
		Dim Stream As New MemoryStream
		Dim Bytes As Byte()
		If Encrypt Then
			Bytes = Encoding.UTF8.GetBytes(Text)
			Dim cs As New CryptoStream(Stream, Encryptor.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
			cs.Write(Bytes, 0, Bytes.Length)
			cs.FlushFinalBlock()
			Return Convert.ToBase64String(Stream.ToArray())
		Else
			Text = Text.Replace(" ", "+")
			Bytes = Convert.FromBase64String(Text)
			Dim cs As New CryptoStream(Stream, Encryptor.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
			cs.Write(Bytes, 0, Bytes.Length)
			cs.FlushFinalBlock()
			Return System.Text.Encoding.UTF8.GetString(Stream.ToArray())
		End If
	End Function

End Module
